package com.blt.car.me;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.utils.StringUtils;
import com.blt.car.widget.dialog.ConvertDialog;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.purse.PayApi;
import blt.app_core.api.purse.bean.CouponVo;
import blt.app_core.api.purse.bean.PurseVo;
import blt.app_core.log.Logger;

/**
 * Created by xgp on 2017/6/27.
 */

public class MyCouponActivity extends BaseTitleBarActivity {
    public static final String COUPON_NUMS = "com.blt.car.me.coupon_nums";

    private LayoutInflater inflater;
    private ExpandableListView expandableListView;

    private List<String> groupList = new ArrayList<>();
    private List<CouponVo> discountList = new ArrayList<>();
    private List<CouponVo> chitList = new ArrayList<>();

    private TextView tcAmount;

    private int couponNums;
    CouponAdapter couponAdapter;
//    ConvertDialog convertDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_coupon;
    }

    @Override
    protected void initData() {
        super.initData();
        groupList.add("代金卷");
        groupList.add("折扣卷");
        PurseVo vo = (PurseVo) getIntent().getExtras().getSerializable(COUPON_NUMS);
        couponNums = vo.couponCount;
        CoreManager.getCore(PayApi.class).getCouponList(1, 10);
    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("我的优惠卷");
        mTitleBar.setRightText("使用记录");
        mTitleBar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        expandableListView = holder.get(R.id.coupon_expand_list);
        inflater = LayoutInflater.from(this);
        couponAdapter = new CouponAdapter();
        expandableListView.setAdapter(couponAdapter);
        couponAdapter.notifyDataSetChanged();

        tcAmount = holder.get(R.id.coupon_amount);
        tcAmount.setText(couponNums + "");

        holder.get(R.id.coupon_btn_submit);
        holder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConvertDialog convertDialog = new ConvertDialog(MyCouponActivity.this);
                convertDialog.show();
            }
        }, R.id.coupon_btn_submit);
    }


    @CoreEvent(coreClientClass = PayApi.class)
    public void onCouponList(List<CouponVo> data) {
        for (CouponVo couponVo : data) {
            if (couponVo.couponType == 1) {
                discountList.add(couponVo);
            } else {
                chitList.add(couponVo);
            }
        }
        couponAdapter.notifyDataSetChanged();
    }


    @CoreEvent(coreClientClass = PayApi.class)
    public void onConvert() {
        toastShort("兑换成功");
    }

    class CouponAdapter extends BaseExpandableListAdapter {

        @Override
        public int getGroupCount() {
            return groupList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return groupPosition == 0 ? chitList.size() : discountList.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupList.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View group, ViewGroup parent) {
            if (group == null) {
                group = inflater.inflate(R.layout.item_coupon_group, parent, false);
            }
            TextView stName = (TextView) group.findViewById(R.id.item_group_start_name);
            TextView tvNums = (TextView) group.findViewById(R.id.item_group_coupon_nums);
            stName.setText(groupList.get(groupPosition));
            if (groupPosition == 0) {
                tvNums.setText(chitList.size() + "张");
            } else {
                tvNums.setText(discountList.size() + "张");
            }
            return group;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View child, ViewGroup parent) {
            if (child == null) {
                child = inflater.inflate(R.layout.item_coupon_child, parent, false);
            }
            RelativeLayout itemBg = (RelativeLayout) child.findViewById(R.id.item_child_bg);
            TextView tvNums = (TextView) child.findViewById(R.id.item_coupon_roll_nums);
            TextView tvYuan = (TextView) child.findViewById(R.id.item_coupon_yuan);
            TextView tvName = (TextView) child.findViewById(R.id.item_coupon_child_introduce);
            TextView tvNumber = (TextView) child.findViewById(R.id.coupon_number);
            TextView tvTime = (TextView) child.findViewById(R.id.coupon_expire_time);

            if (groupPosition == 1) {//折扣
                itemBg.setBackgroundResource(R.mipmap.bg_reduce);
                tvNums.setTextColor(getResources().getColor(R.color.text_color_blue));
                tvYuan.setTextColor(getResources().getColor(R.color.text_color_blue));
                tvName.setText(discountList.get(childPosition).couponDetail);
                DecimalFormat df = new DecimalFormat("0");
                String amount = df.format(discountList.get(childPosition).subAmt);
                tvNums.setText(amount);
                int id = discountList.get(childPosition).id;
                DecimalFormat decimalFormat = new DecimalFormat("C0000100");
                tvNumber.setText("卷号" + decimalFormat.format(id));
                tvTime.setText("有效期至" + StringUtils.getYear(discountList.get(childPosition).expireTime));

            } else {//代金
                itemBg.setBackgroundResource(R.mipmap.bg_voucher);
                tvNums.setTextColor(getResources().getColor(R.color.text_color_orange));
                tvYuan.setTextColor(getResources().getColor(R.color.text_color_orange));

                tvName.setText(chitList.get(childPosition).couponDetail);
                DecimalFormat df = new DecimalFormat("0");
                String amount = df.format(chitList.get(childPosition).subAmt / 100);
                tvNums.setText(amount);
                int id = chitList.get(childPosition).id;
                DecimalFormat decimalFormat = new DecimalFormat("C0000100");
                tvNumber.setText("卷号" + decimalFormat.format(id));
                tvTime.setText("有效期至" + StringUtils.getYear(chitList.get(childPosition).expireTime));
            }

            return child;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }
}
